<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class CateController extends Controller
{
    //add
    public function add()
    {
        //选择出id name path 字段,并且数据按照path排序
    	$data = DB::table('cate') -> select('id','name','path',DB::raw("concat(path,',',id) AS sort_str")) -> orderBy('sort_str') -> get();
        
        //遍历获取path(给path分层)
        foreach($data as $key => $value)
        {
            
            $num = substr_count($value -> path ,',');
            $str = str_repeat('|----',$num);
            $data[$key] -> name = $str.$value -> name;
        }
        
    	return view('admin.cate.add',['data' => $data]);
    }

    //insert
    public  function insert(Request $request)

    {
    	$data = $request -> except('_token');

        if($data['pid'] == 0)
        {
            $path = '0';
        }else
        {
            //获取数据表中id= pid的数据
            $pdata = DB::table('cate') -> where('id',$data['pid']) -> first();
            //拼装新的路径
            $path = $pdata -> path.','.$data['pid'];
          
        }
            $data['path'] = $path;
            
            //处理图片
            if($request -> hasFile('logo'))
            {
                if($request -> file('logo') -> isValid())
                {
                    //获取图片扩展名
                    $extension = $request -> file('logo') -> getClientOriginalExtension();

                    //拼接图片名称
                    $fileName = str_random(32).'.'.$extension;
                    $dir = './uploads/imgs/';

                    //判断目录是否存在
                    if(!file_exists($dir))
                    {
                        mkdir($dir,0777,true);
                    }

                    //移动
                    $move = $request -> file('logo') -> move($dir,$fileName);

                    //判断是否移动成功
                    if($move)
                    {
                        $data['logo'] = $fileName;
                        
                    }
                }
            }

            //将数据插入数据库
            $res = DB::table('cate') -> insert($data);

            if($res)
            {
                return redirect('/admin/cate/index') -> with(['info' => '添加成功']);
            }else
            {
                return back() -> with(['info' => '添加失败']);
            }

        
    }

    //index
    public function index()
    {
        
        //选择出id name path 字段,并且数据按照path排序
        $data = DB::table('cate') -> select('id','name','path',DB::raw("concat(path,',',id) AS sort_str")) -> orderBy('sort_str') -> get();
        //var_dump("<pre>",$data);die;
        //遍历获取path(给path分层)
        foreach($data as $key => $value)
        {
            
            $num = substr_count($value -> path ,',');
            $str = str_repeat('|----',$num);
            $data[$key] -> name = $str.$value -> name;
        }
        return view('admin.cate.index',['data' => $data]);
    }

    public function edit($id)
    {
         $allData = DB::table('cate') -> select('id','name','path',DB::raw("concat('path,id') AS sort_str")) -> orderBy('sort_str') -> get();
         //var_dump('<pre>',$allData);die();
        foreach($allData as $key => $value)
        {
            $num = substr_count($value -> path,',');
            $str = str_repeat('|----',$num);
            $allData[$key] -> name = $str.$value -> name;
        }

        $data = DB::table('cate') -> where('id',$id) -> first();
        return view('admin.cate.edit',['data' => $data,'allData' => $allData]);
    }

    //update 
    public function update(Request $request,$id)
    {
        $data = $request -> except('_token');
        //var_dump($data);die;
        if($data['pid'] == 0)
        {
            $path = '0';

        }else
        {
            //获取数据库中id = pid数据
            $pdata = DB::table('cate') -> where('id',$data['pid']) -> first(); 
            //拼装新的路径                                                      
            $path = $pdata -> path .','.$data['pid'];
            //var_dump($path);die;
        }
        $data['path'] = $path;

        //执行修改
        $res = DB::table('cate') -> where('id',$id) -> update(["path"=>$path]);
        // var_dump($res);die;
        if($res)
        {
            return redirect('/admin/cate/index') -> with(['info' => '编辑成功']);
        }else
        {
            return back() -> with(['info' => '编辑失败']);
        }
    }

    //delete删除
    public function delete()
    {
        //查询是否有子分类
        $res = DB::table('cate') -> where('pid',$_GET['id']) -> first();
        if($res)
        {
            echo json_encode('含有子类无法删除');
        }else
        {
            //执行删除    
            $res = DB::table('cate') -> delete($_GET["id"]);
            if($res)
            {
                 echo json_encode('删除成功');
            }else
            {
                 echo json_encode('删除失败');
            }

        }
        
    }
}